package com.example.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.entity.Cart;
import com.example.shop.mapper.CartMapper;
import com.example.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private Cart cart;
    @Override
    public int insertCart(Cart cart) {
        return cartMapper.insert(cart);
    }

    @Override
    public Cart getCart(int memberId, int goodsId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("goods_id", goodsId);
        return cartMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateCart(int memberId, int goodsId, int quantity) {
        return cartMapper.updateCart(memberId, goodsId,quantity);
    }

    @Override
    public List<Cart> getCartListByMemberId(int memberId) {
        return cartMapper.selectList(new QueryWrapper<Cart>().eq("member_id", memberId));
    }

    @Override
    public Double getCartTotalAmount(int memberId) {
        return cartMapper.getCartTotalAmount(memberId);
    }

    @Override
    public int deleteCartGoodsById(int id) {
        return cartMapper.deleteById(id);
    }

    @Override
    public int updateSelectedById(Long id,int selected) {
        return cartMapper.updateSelectedById(id,selected);
    }
    //验证购物车是否有选中商品
    @Override
    public List<Cart> getCartListByMemberIdSelected(int memberId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("selected", 1);
        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public int batchUpdateSelected(String ids,int selected) {
        return cartMapper.batchUpdateSelected(ids,selected);
    }

    @Override
    public int updateCartQuantity(int id, int quantity) {
        return cartMapper.updateCartQuantity(id,quantity);
    }
}
